/*******************************************************************************
 * SVN­Versioning-Information:
 * ­­­­­­­­­­­­­­­­­­­­­­­­
 * $LastChangedRevision: 6 $
 * $LastChangedBy: c.a.duevel $
 * $LastChangedDate: 2009-08-26 15:12:37 +0000 (Wed, 26 Aug 2009) $
 * $HeadURL: http://robofight.googlecode.com/svn/trunk/ClientJava/src/de/hft_stuttgart/robofight/client/java/JLogin.java $
 ******************************************************************************/
package de.hft_stuttgart.robofight.client.java;

import de.hft_stuttgart.botwar.common.interfaces.IRemoteLogin;
import de.hft_stuttgart.botwar.common.interfaces.IRemotePlayer;
import de.hft_stuttgart.botwar.common.models.Password;
import de.hft_stuttgart.botwar.exceptions.RegistrationUnsuccessfulException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.security.auth.login.LoginException;

/**
 *
 * @author ralf.dauenhauer@gmx.de, michael.rieker@web.de
 */
public class JLogin {

    private String local = "";
    private IRemoteLogin login;
    private static final int PORT = 1099;
    private String loginException = "";

    /**
     * creates a new JLogin object.
     */
    public JLogin() {
    }

    /**
     * Is necessary to start client on localhost or remote.
     * @param local contains the arguments from ide
     */
    public final void setLocal(final String local) {
        this.local = local;
    }

    /**
     * connect to rmi registry on the server.
     * @return returns true if connection was successfull
     * @throws java.net.MalformedURLException throws a MalformedURLException
     */
    public final String connect() {
        String ret = null;
        try {
            System.out.println("ServerIP: " + local);
            java.rmi.RMISecurityManager sm =
                    new java.rmi.RMISecurityManager();
            Registry reg = LocateRegistry.getRegistry(
                    local, PORT);
            login = (IRemoteLogin) reg.lookup("login");
            ret = null;
        } catch (NotBoundException ex) {
            Logger.getLogger(
                    JLogin.class.getName()).log(Level.SEVERE, null, ex);
            ret = ex.getMessage();
        } catch (RemoteException ex) {
            Logger.getLogger(
                    JLogin.class.getName()).log(Level.SEVERE, null, ex);
            ret = ex.getMessage();
        } finally {
            if (ret != null && ret.length() > 80) {
                String tmp1 = ret.substring(0, 79);
                String tmp2 = ret.substring(80, ret.length());
                ret = tmp1 + "\n" + tmp2;
            }
            return ret;
        }
    }

    /**
     * login to RMI registry.
     * @param userName needs the username to login
     * @param password needs the password to login
     * @return player, returns RemoteReference of type IRemotePlayer
     * @throws javax.security.auth.login.LoginException throws LoginException
     */
    public final IRemotePlayer login(final String userName,
            final String password) {

        String name = userName;
        String pw = password;
        try {
            return login.login(name, new Password(pw));
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(
                    JLogin.class.getName()).log(Level.SEVERE, null, ex);
            loginException = ex.getMessage();
        } catch (LoginException ex) {
            Logger.getLogger(
                    JLogin.class.getName()).log(Level.SEVERE, null, ex);
            loginException = ex.getMessage();
        } catch (RemoteException ex) {
            Logger.getLogger(
                    JLogin.class.getName()).log(Level.SEVERE, null, ex);
            loginException = ex.getMessage();
        }
        return null;
    }

    /**
     * Returns true, if the password is the same as the confirmed password.
     * @param pwd contains the pwd
     * @param confPwd contains the confirmed pwd
     * @return boolean true, if password and confirmed password are equal
     */
    public final boolean checkInput(final String pwd, final String confPwd) {
        boolean ret = false;
        if (pwd.equals(confPwd)) {
            ret = true;
        }
        return ret;
    }

    /**
     * Register the new player account.
     * Register the new player account.
     * @param userid needs the userid as param for register
     * @param pwd needs the pwd as param for register
     * @param email needs the email as param for register
     * @return boolean if register was successful or not
     */
    public final String register(final String userid, final String pwd,
            final String email) {
        try {
            login.register(userid, new Password(pwd), email);
            return "";
        } catch (NoSuchAlgorithmException ex) {
            return ex.getMessage();
        } catch (RemoteException ex) {
            return ex.getMessage();
        } catch (RegistrationUnsuccessfulException ex) {
            return ex.getMessage();
        } catch (IllegalArgumentException ex) {
            return ex.getMessage();
        }
    }

    /**
     * Returns the exception message as string.
     * @return loginException the exception message as string
     */
    public final String getException() {
        return loginException;
    }
}
